<template>
  <div class="container">
    <!-- 添加博客 -->
    <el-button type="primary" size="small" class="add-button" @click="openAddDialog()">添加文章</el-button>
    <!-- 搜索 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="博客标题">
        <el-input v-model="pageBean.params.blogTitle" placeholder="博客标题" />
      </el-form-item>
      <el-form-item label="博客分类">
        <el-select v-model="pageBean.params.typeId" clearable placeholder="博客分类">
          <el-option
            v-for="type in typeList"
            :key="type.typeId"
            :label="type.typeName"
            :value="type.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="findByPage">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 分割线 -->
    <el-divider />
    <!-- 列表 -->
    <el-table :data="pageBean.list" border style="width: 100%" @sort-change="changeSort">
      <!-- <el-table-column prop="blogId" label="ID" /> -->
      <el-table-column prop="blogTitle" label="标题" width="100" show-overflow-tooltip />
      <el-table-column prop="typeName" label="分类" sortable="custom" />
      <el-table-column prop="blogImage" label="图片">
        <template slot-scope="scope">
          <el-image
            style="width: 100%; height: 100px"
            :src="scope.row.blogImage"
            :preview-src-list="[scope.row.blogImage]"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="blogContent" label="内容" /> -->
      <el-table-column prop="blogGoods" label="点赞数" width="100" sortable="custom" />>
      <el-table-column prop="blogRead" label="阅读数" width="100" sortable="custom" />
      <el-table-column prop="blogCollection" label="收藏数" width="100" sortable="custom" />
      <el-table-column prop="blogComment" label="评论数" width="100" sortable="custom" />
      <el-table-column prop="blogSource" label="文章来源" width="100" />
      <el-table-column prop="createdTime" label="创建时间" sortable="custom" />
      <el-table-column prop="updateTime" label="修改时间" sortable="custom" />
      <el-table-column prop="blogRemark" label="备注" width="100" show-overflow-tooltip />
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="openUpdateDialog(scope.row.blogId)">修改</el-button>
          <el-button size="mini" type="danger" @click="deleteBlog(scope.row.blogId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block fenye">
      <!-- <span class="demonstration">完整功能</span> -->
      <el-pagination
        background
        :current-page="pageBean.pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        配置每页显示几条
        :page-size="pageBean.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageBean.totalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 分页 end-->
    <!-- 添加弹窗 -->
    <el-dialog
      title="添加文章"
      :visible.sync="addDialog"
    ><blog-add @closeAddDialog="closeAddDialog" @findByPage="findByPage" />
    </el-dialog>

    <!-- 修改弹窗 -->
    <el-dialog
      title="修改文章"
      :visible.sync="updateDialog"
    ><blog-update :blog="blog" @closeUpdateDialog="closeUpdateDialog" @findByPage="findByPage" />
    </el-dialog>
  </div>
</template>
<script>
import blogApi from '@/api/blog'
import blogAdd from './blog-add'
import blogUpdate from './blog-update'
export default {
  // 注册组件
  components: {
    blogAdd,
    blogUpdate
  },
  data() {
    return {
      pageBean: {
        pageNumber: 1, // 当前页
        pageSize: 10, // 每个显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [],
        sortColumn: 'created_time', // 排序列
        sortMethods: 'asc'
      },
      addDialog: false, // 控制修改弹窗
      updateDialog: false, // 控制添加弹窗
      // 从全局去除 typeList
      typeList: this.$store.getters.typeList,
      blog: {
        blogId: '',
        blogTitle: '',
        blogImage: null,
        blogContent: null,
        blogGoods: 0,
        blogRead: 0,
        blogCollection: 0,
        typeName: '',
        blogSource: null,
        blogRemark: null,
        blogComment: 0,
        createTime: '',
        updateTime: ''
      }
    }
  },
  created() {
    this.findByPage()
    // 延时加载查询中的分类 下拉
    setTimeout(() => {
      this.typeList = this.$store.getters.typeList
    }, 200)
  },
  methods: {
    findByPage() {
      blogApi.findByPage(this.pageBean).then(response => {
        console.log(response)
        this.pageBean = response.data
      })
    },
    openAddDialog() {
      // 监听打开添加弹窗
      this.addDialog = true
    },
    closeAddDialog() {
      // 关闭添加弹窗
      this.addDialog = false
    },
    handleSizeChange(val) {
      // 分页显示每页多少条 触发
      this.pageBean.pageSize = val
      this.findByPage()

      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      // 当前页触发
      this.pageBean.pageNumber = val
      this.findByPage()

      console.log(`当前页: ${val}`)
    },
    openUpdateDialog(id) {
      // 监听打开修改弹窗
      blogApi.findById(id).then(res => {
        this.blog = res.data
        console.log(res.data)
        this.updateDialog = true
      })
    },
    closeUpdateDialog() {
      // 关闭修改弹窗
      this.updateDialog = false
    },
    deleteBlog(id) {
      // 删除博客
      this.$confirm('此操作将永久删除！, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        blogApi.deleteById(id).then(response => {
          this.$message.success(response.msg)
          // 删除成功 刷新博客列表
          this.findByPage()
        })
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    changeSort(e) {
      // 排序
      console.log(e)
      this.pageBean.sortColumn = e.prop
      this.pageBean.sortMethods = e.order
      this.findByPage()
    }
  }
}
</script>
<style scoped>
.fenye{
  margin-top: 15px;
}
</style>
